package com.itheima.controller;

import com.itheima.domian.Student;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
private StudentService studentService;
    @RequestMapping("/login")
public String login(Student student) throws IOException {
        Student login = studentService.login(student);
        if(login!=null){
            return "redirect:/student/index1";
        }

        return "error";
}
@RequestMapping("/index1")
public ModelAndView index() throws IOException {
    List<Student> list = studentService.findAll();
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("list",list);
    modelAndView.setViewName("index");
    return modelAndView;
}
@RequestMapping("/delete")
public String delete(Integer cid) throws IOException {

        studentService.delete(cid);
        return "redirect:/student/index1";
}
@RequestMapping("/add")
public String add(Student student) throws IOException {
    System.out.println(student);
        studentService.add(student);
        return "redirect:/student/index1";
}
@RequestMapping("/update")
public String update(Student student) throws IOException {
        studentService.update(student);
        return "redirect:/student/index1";
}
    @RequestMapping("/show")
    public ModelAndView update(Integer cid) throws IOException {
        Student student = studentService.findById(cid);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("student",student);
        modelAndView.setViewName("update");
        return modelAndView;

    }
}
